Page({
  data: {
    videoList: [
      { 
        id: 1, 
        title: "初学者完整健身计划 - 30天挑战", 
        src: "../video/video.mp4", 
        poster: "https://picsum.photos/id/237/400/225",
        duration: "15:30",
        author: "健身教练Mike",
        views: "12.5万"
      },
      { 
        id: 2, 
        title: "瑜伽基础入门 - 改善体态和柔韧性", 
        src: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4", 
        poster: "https://picsum.photos/id/238/400/225",
        duration: "22:18",
        author: "瑜伽导师Lisa",
        views: "8.3万"
      },
      { 
        id: 3, 
        title: "HIIT高强度间歇训练 - 快速燃脂", 
        src: "https://media.w3.org/2010/05/sintel/trailer.mp4", 
        poster: "https://picsum.photos/id/239/400/225",
        duration: "10:45",
        author: " fitness工作室",
        views: "21.7万"
      },
      { 
        id: 4, 
        title: "核心力量训练 - 腹肌塑造", 
        src: "http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4", 
        poster: "https://picsum.photos/id/240/400/225",
        duration: "18:22",
        author: "健身达人Alex",
        views: "15.2万"
      }
    ],
    currentId: null // 当前播放的视频ID
  },

  // 播放视频
  playVideo(e) {
    const id = e.currentTarget.dataset.id;
    const { currentId } = this.data;
    
    // 如果点击的是正在播放的视频，则暂停
    if (currentId === id) {
      const videoCtx = wx.createVideoContext(`video${id}`);
      videoCtx.pause();
      this.setData({ currentId: null });
      return;
    }
    
    // 暂停上一个视频
    if (currentId) {
      const prevCtx = wx.createVideoContext(`video${currentId}`);
      prevCtx.pause();
    }
    
    // 播放当前视频
    this.setData({ currentId: id }, () => {
      const videoCtx = wx.createVideoContext(`video${id}`);
      videoCtx.play();
    });
  },
  onFullScreenChange(e) {
    console.log('全屏状态变化', e.detail.fullScreen)
  },
  // 视频播放结束
  onVideoEnded(e) {
    const id = e.currentTarget.id.replace('video', '');
    this.setData({ currentId: null });
  }
});
